﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AgencyEntityLib.BaseEntity;
using Ecopsy.Tools;
using FormControllers;
using Ecopsy.DataTier;

public partial class iFrameAdmin_Group_EditSurfaceGroup : iAdmin_Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        

        if (!IsPostBack)
        {
            ctlSideLst1.EditMode = false;
            MyDataBind();
            
        }
        Init();
    }

    protected string SearhValue
    {
        get {return  ctlSideLst1.Lexeme;}
    }

    protected void SetMsg_EditStatus()
    {
        lblClientStatus.DataBind();
        updStatus.Update();
    }

    protected void Init()
      {

          ctlSideLst1.KeyPref = SurfaceGroupID;
          ctlSideLst1.OnRemove = OnRemoveRecord;

          SetAlertForTextBox(this.Controls, "TextBox", "onChange", "ShowAlert()");
          SetMsg_EditStatus();
      }

    private void MyDataBind()
    {
        DataBind();        
        
    }
    
    protected void btnSaveCommonInfo_Click(object sender, EventArgs e)
    {
        ReadForm();
        wsAgency ws = new wsAgency();
        SurfaceGroup g= ws.SaveSurfaceGroup(SurfaceGroup); 
        if(IsNewRecord) Response .Redirect ("iFrameAdmin_EditSurfaceGroup.aspx?"+qSurfaceGroupID+"="+g.ID) ;  
    }

    private void ReadForm()
    {
        SurfaceGroup.Name = txtName.Text;
        SurfaceGroup.Description = txtComment.Text;

    }
    
    protected bool IsNewRecord 
    {
        get { return SurfaceGroupID.IndexOf(GlobalConstants.qPrefNEW_RECORD) > -1; }
    }
    
    protected string SurfaceGroupID
    {
        get { return Request.QueryString[qSurfaceGroupID];} 
    }
    
    protected SurfaceGroup SurfaceGroup     
    {
        get {

                wsAgency ws = new wsAgency();
                return ws.GetSurfaceGroup (SurfaceGroupID);
        }
        
    }


    protected override void OnDataBinding(EventArgs e)
    {     
        ctlSideLst1.FilterList = SurfaceGroup.SurfaceList;               
        base.OnDataBinding(e);
    }

    protected void btnFind_Click(object sender, ImageClickEventArgs e)
    {

        string sFlt = txtSearch.Text;        
        //int errPosition = -1;
        //if (!SqlLibrary.ExpressionChecker.CheckStatment(ref sFlt, ref errPosition))
        //{
        //    ScriptManager.RegisterStartupScript(this, GetType(), "AlertBadFiltr", "alert('Ошибочное выражение фильтрации');", true);
        //    return;
        //}

        ctlSideLst1.Lexeme = sFlt; 
        MyDataBind ();
    }
    
    protected void BtnSave_Click(object sender, EventArgs e)
    {
        if (ctlSideLst1.EditMode)
        {
            SurfaceGroup.SurfaceList = ctlSideLst1.FilterList;
        }        

        // сбросить DataPage 
        ctlSideLst1.ResetPager();
        ctlSideLst1.EditMode = !ctlSideLst1.EditMode;                
        ctlSideLst1.Lexeme = string.Empty;
        btnSaveCommonInfo_Click(null, null);
        DataBind();

    }

    
    protected string GetSaveButtontext()
    {
        if (ctlSideLst1.EditMode) return "Сохранить";
        return "Редактировать";
    }

    protected string GetStateVisible()
    {   
        if (SurfaceGroup.State != EntityState.Loaded) return string.Empty;        
        foreach (Entity item in SurfaceGroup.SurfaceList )
        {
            if (item.State != EntityState.Loaded) return string.Empty;
        }
        return "hidden";

    }

    protected bool OnRemoveRecord(string RecordId)
    {
        SurfaceGroup.SurfaceList.Remove(RecordId);
        btnSaveCommonInfo_Click(null, null);
        DataBind();
        return true;
    }
}
